package cn.cs.service.impl;


import cn.cs.client.RestUserAddressClient;
import cn.cs.common.*;
import cn.cs.pojo.UserAddress;
import cn.cs.service.UserAddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Resource
    private RestUserAddressClient userAddressClient;
    @Resource
    private RedisUtils redisUtils;

    @Override
    public List<UserAddress> getUserAddressById(String userId)throws Exception{
        return userAddressClient.getUserAddressById(userId);
    }

    @Override
    public UserAddress getUserAddressByOnceId(String id)throws Exception{
        return userAddressClient.getUserAddressByOnceId(id);
    }

    @Override
    public List<UserAddress> getUserAddressListById(Map<String,Object> param)throws Exception{
        return userAddressClient.getUserAddressListById(param);
    }
    @Override
    public List<UserAddress> getUserAddressListByMap(Map<String,Object> param)throws Exception{
        return userAddressClient.getUserAddressListByMap(param);
    }
    @Override
    public Integer getUserAddressCountByMap(Map<String,Object> param)throws Exception{
        return userAddressClient.getUserAddressCountByMap(param);
    }

    public Integer itriptxAddUserAddress(UserAddress userAddress)throws Exception{
//            userAddress.setCreationDate(new Date());
            return userAddressClient.itriptxAddUserAddress(userAddress);
    }

    public Integer itriptxModifyUserAddress(UserAddress userAddress)throws Exception{
//        userAddress.setModifyDate(new Date());
        return userAddressClient.itriptxModifyUserAddress(userAddress);
    }

    public Integer modifyUserAddressByUserId(UserAddress userAddress)throws Exception{
        return userAddressClient.modifyUserAddressByUserId(userAddress);
    }

    @Override
    public void itriptxDeleteUserAddressById(String id)throws Exception{
        userAddressClient.itriptxDeleteUserAddressById(id);
    }
    @Override
    public Page<UserAddress> queryUserAddressPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = userAddressClient.getUserAddressCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<UserAddress> userAddressList = userAddressClient.getUserAddressListByMap(param);
        page.setRows(userAddressList);
        return page;
    }

    @Override
    public Dto registerAddress(HttpServletRequest request) throws Exception {
        String userId = request.getParameter("userId");
        if(EmptyUtils.isEmpty(userId)){
            return DtoUtil.returnFail("用户不存在，请重新授权登陆", "userNull001");
        }
//        String userId = "1";
        String addressTypeId=request.getParameter("addressTypeId");
        UserAddress userAddress=new UserAddress();
        String addressId = IdWorker.getId();
        userAddress.setId(addressId);
        userAddress.setUserId(userId);
        userAddress.setUserName(request.getParameter("userName"));
        userAddress.setUserPhone(request.getParameter("userPhone"));
        userAddress.setAddressContent(request.getParameter("addressContent"));
        userAddress.setProvince(request.getParameter("province"));
        userAddress.setCity(request.getParameter("city"));
        userAddress.setArea(request.getParameter("area"));
        userAddress.setAddressTypeId(addressTypeId);
        int count = userAddressClient.itriptxAddUserAddress(userAddress);
        if("1".equals(addressTypeId)){
            userAddressClient.updateAddressTypeId(addressId);
        }
        if(count >0){
            return DtoUtil.returnSuccess("插入成功",addressId);
        }else{
            return DtoUtil.returnFail("插入失败","false");
        }
    }

    @Override
    public Dto updateUserAddress(HttpServletRequest request) throws Exception {
        String id=request.getParameter("id");
        String userName=request.getParameter("userName");
        String userPhone=request.getParameter("userPhone");
        String addressContent=request.getParameter("addressContent");
        String province=request.getParameter("province");
        String city=request.getParameter("city");
        String area = request.getParameter("area");
        String addressTypeId=request.getParameter("addressTypeId");
        UserAddress userAddress=new UserAddress();
        userAddress.setId(id);
        userAddress.setUserName(userName);
        userAddress.setUserPhone(userPhone);
        userAddress.setProvince(province);
        userAddress.setCity(city);
        userAddress.setArea(area);
        userAddress.setAddressContent(addressContent);
        userAddress.setAddressTypeId(addressTypeId);
        int n=userAddressClient.modifyUserAddressByUserId(userAddress);

        if(n>0){
            return DtoUtil.returnDataSuccess(userAddress);
        }else{
            return DtoUtil.returnFail("创建失败","002");
        }
    }

    @Override
    public Dto updateAddressTypeId(HttpServletRequest request) throws Exception {
        String id=request.getParameter("id");
        userAddressClient.updateAddressTypeId(id);
        return DtoUtil.returnDataSuccess("操作成功");
    }

}
